package com.jojo.bubble.utils;

import com.jojo.bubble.entity.Admin;
import lombok.extern.log4j.Log4j2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.*;


/**
 * @author: jojo
 * @date: 2022/11/14 10:46
 * @desc: 后台通用
 */
@Log4j2
public class AdminUtil {
    public static Admin getLoginUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (null == session) {
            return null;
        }
        return (Admin) session.getAttribute("admin");
    }

    static ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(20);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        // 等1s
        Callable ca1 = () -> {
            try {
                Thread.sleep(1000);
                System.out.println(1);
//                executorService.shutdown();
                System.out.println("c1:"+newFixedThreadPool);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        };


        //等3s
        Callable ca2 = () -> {
            try {
                Thread.sleep(3000);
                System.out.println(3);

//                executorService.shutdown();
                System.out.println("c2:"+newFixedThreadPool);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 3;
        };
        Callable ca3 = () -> {
            try {
//                executorService.shutdown();
                Thread.sleep(10000);
                System.out.println(10);

                System.out.println("c3:"+newFixedThreadPool);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        };
        FutureTask<Integer> ft1 = new FutureTask<Integer>(ca1);
        newFixedThreadPool.submit(ft1);
        System.out.println(newFixedThreadPool);


        FutureTask<Integer> ft2 = new FutureTask<Integer>(ca2);
        newFixedThreadPool.submit(ft2);

        System.out.println(newFixedThreadPool);
        FutureTask<Integer> ft3 = new FutureTask<Integer>(ca3);
        newFixedThreadPool.submit(ft3);

        long end = System.currentTimeMillis();

        System.out.println(newFixedThreadPool);
    }
}
